/*
 * Copyright (c) 2015, FPX and/or its affiliates. All rights reserved.
 * Use, Copy is subject to authorized license.
 */

package com.commerceerp.system.api.fourpx;

import com.commerceerp.system.util.MD5Tools;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * API请求
 *
 * @author fc
 * @date 2018/12/13
 */
public class ApiRequest implements Serializable {

    public ApiRequest() {
    }
    
    public String getISO8601Timestamp(Date date){  
        TimeZone tz = TimeZone.getTimeZone("UTC");  
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");  
        df.setTimeZone(tz);  
        String nowAsISO = df.format(date);  
        return nowAsISO;  
    }  
    
    public ApiRequest(String method, String appKey, String appSecret, String body) {
        this.method = method;
        this.appKey = appKey;
        this.appSecret = appSecret;
        this.format = "json";
        this.v = "1.0.0";
        this.timestamp = getISO8601Timestamp(new Date());
        this.body = body;
    }
    

    /**
     * 生成待签名的字符串
     * @return
     */
    private String generateSignParamString() {
        StringBuffer sb = new StringBuffer();
        sb.append("app_key").append(this.appKey).append("format").append(this.format).append("method").append(this.method).append
                ("timestamp").append(this.timestamp).append("v").append(this.v).append(this.body).append(this.appSecret);
        return sb.toString();
    }

    /**
     * 生成签名值
     */
    public void generateSignValue() {
        String sign_str = generateSignParamString();
        System.out.println("sign_str:"+sign_str);
        this.sign = MD5Tools.MD5(sign_str);
        this.sign = this.sign.toLowerCase();
        System.out.println("sign:"+sign);
    }
    

    /**
     * 生成请求路径
     * @param apiUrl
     * @param accessToken
     * @return
     * @throws UnsupportedEncodingException
     */
    public String generateUrl(String apiUrl,String accessToken) throws UnsupportedEncodingException {
        StringBuffer url= new StringBuffer();
        url.append(apiUrl);
        url.append("?");
        url.append("method").append("=").append(this.method);
        url.append("&").append("v").append("=").append(this.v);
        url.append("&").append("app_key").append("=").append(this.appKey);
        url.append("&").append("timestamp").append("=").append(URLEncoder.encode(this.timestamp, "UTF-8") );
        url.append("&").append("format").append("=").append(this.format);
        url.append("&").append("access_token").append("=").append(accessToken);
        url.append("&").append("sign").append("=").append(this.sign);
        return url.toString();
    }
    /**
     * 服务名
     */
    private String method;
    /**
     * 时间
     */
    private String timestamp;
    /**
     * 分配给客户端的唯一ID
     */
    private String appKey;
    /**
     * 分配给客户端的密钥
     */
    private String appSecret;
    /**
     * 数据格式
     */
    private String format;
    /**
     * 版本号
     */
    private String v;
    /**
     * 签名值 MD5
     */
    private String sign;

    /**
     * 业务参数体
     */
    private String body;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public String getAppKey() {
        return appKey;
    }

    public void setAppKey(String appKey) {
        this.appKey = appKey;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(String format) {
        this.format = format;
    }

    public String getV() {
        return v;
    }

    public void setV(String v) {
        this.v = v;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }
    
    public static void main(String[] args) throws Exception {

    }
}
